<template>
	<view class="cotent">
		<view class="search-view">
			<view class="flex-col search-item">
				<view class="flex-row search-title">
					<text>条码采集</text>
					<text class="search-clean" @tap="cleanCode">清空</text>
				</view>
				<view class="search-input">
					<input class="search-input" type="text" 
						placeholder-class="input-placeholder" placeholder="扫码或手动输入"
						v-model="searchVal" :focus="isFocus" @confirm="confirmScan" @keyup.enter="confirmScan"
						>
				</view>
			</view>
		</view>
		<view class="submit-btn" @tap="toUncheckGoodsList()">
			去对码
		</view>
		<!-- 对码成功弹窗 -->
		<uni-popup ref="successPopup" type="center">
			<view class="flex-col success-good-popup">
				<view class="success-tip">
					<text>此商品已对码</text>
				</view>
				<view class="success-good-content">
					<text>{{goodDetails.Code}}</text>
				</view>
				<view class="success-good-content">
					<text>{{goodDetails.Name}}</text>
				</view>
				<view class="success-good-content">
					<text>{{goodDetails.Manufacturer}}</text>
				</view>
				<view class="success-good-content">
					<text>{{goodDetails.Spec}}</text>
				</view>
				<view class="success-good-content">
					<text>{{goodDetails.ApprovalNumber}}</text>
				</view>
				<view class="flex-row popup-btn">
					<view class="popup-left-btn" @tap="reCheck()">
						重新对码
					</view>
					<view class="popup-right-btn" @tap="closeSuccessPopup()">
						确定
					</view>
				</view>
			</view>
		</uni-popup>
		<!-- 是否重新对码弹窗 -->
		<uni-popup ref="recheckPopup" type="center">
			<view class="flex-col success-good-popup">
				<view class="success-tip">
					<text>是否重新对码</text>
				</view>
				<view class="success-good-content">
					<text>{{goodDetails.Code}}</text>
				</view>
				<view class="success-good-content">
					<text>{{goodDetails.Name}}</text>
				</view>
				<view class="success-good-content">
					<text>{{goodDetails.Manufacturer}}</text>
				</view>
				<view class="success-good-content">
					<text>{{goodDetails.Spec}}</text>
				</view>
				<view class="success-good-content">
					<text>{{goodDetails.ApprovalNumber}}</text>
				</view>
				<view class="flex-row popup-btn">
					<view class="popup-left-btn" @tap="closeRecheckPopup()">
						取消
					</view>
					<view class="popup-right-btn" @tap="confirmRecheck()">
						确定
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { mapState, mapMutations } from 'vuex'
	var that;
	export default {
		data() {
			return {
				searchVal: '',
				pageSize:100,
				pageNum:1,
				isFocus: false,
				goodDetails: {
					"Code": "",
					"Name": "",
					"Spec": "",
					"Unit": "",
					"ApprovalNumber": "",
					"Manufacturer": "",
					"CodeKind": "",
					"IsSupervisionCode": "0",
				}
			}
		},
		computed: {
			...mapState({
				userInfo: state => state.userInfo, // userInfo
				Token: state => state.Token, // Token
				scanGoodsCode: state => state.goods.scanGoodsCode,  // scanGoodCode
			})  
		},
		watch:{
			$route:{
				handler(to,from){
					console.log(to,from)
					that.closeAll();
					that.searchFocus();
				}
			}
		},
		onLoad() {
			that = this
		},
		onShow() {
			that.cleanCode()
			that.getServerData()
		},
		methods: {
			...mapMutations(['initScanGoodsCode', 'cleanScanGoodsCode']),
			getServerData() {
				
			},
			searchBlur() {
				that.isFocus = false
			},
			searchFocus() {
				that.isFocus = true
				uni.hideKeyboard()
				
			},
			searchInput(e) {
				that.code = e.detail.value
			},
			confirmScan(e) {
				//that.searchVal = e.detail.value
				//that.initScanGoodsCode(that.searchVal.trim());
				//that.searchVal = ''
				that.handleCheckCode()
			},
			cleanCode() {
				//that.initScanGoodsCode('');
				setTimeout(() => {
					that.searchVal = ''
				}, 0)
				that.inputFoucs()
				// that.cleanScanGoodsCode(that.code);
				// that.code = ''
			},
			toUncheckGoodsList() {
				if (that.searchVal.length != 20) {
					that.$common.showNormalToast('请输入溯源码');
					return
				}
				
				
				let url = {
					path: '/pagesContrast/uncheckGoodsList/uncheckGoodsList', 
					query: { 
						SupplierCode: '',//that.$Route.query.SupplierCode,
						SupervisionCode: that.searchVal,
						scanType:1
					}
				}
				that.$Router.push(url)
			},
			toGoodsList() {
				let url = {
					path: '/pagesContrast/goodsList/goodsList', 
					query: { 
						title: '', 
						type: '',
					}
				}
				that.$Router.push(url)
			},
			handleCheckCode() {
				
				// if (that.searchVal.replace('\r','').replace('\n').length < 20) {
				// 	that.$common.showNormalToast('请扫溯源码');
				// 	return
				// }
				that.checkCode()
			},
			checkCode() {
				// 判定是否对码
				let query = {
					SupplierCode: that.$Route.query.SupplierCode,
					SupervisionCode: that.searchVal.substring(0,7),
					Keyword:''
				}
				let requestData = {
					pagination: {
						rows: that.pageSize,
						page: that.pageNum
					},
					queryJson: JSON.stringify(query)
				};
				uni.showLoading({
					title: '加载中'
				});
				//console.log(requestData,'请求')
				that.$request.globalRequest('goods/checklist', 'POST', requestData, '', 0).then(res => {
					uni.hideLoading()
					//console.log(res);
					let data = res
					if (data.length > 0) {
						that.goodDetails = data[0]
						that.openPopup('successPopup')
					}
					else
					{
						uni.showModal({
							title: '提示',
							confirmText: '去对码',
							content: '该溯源码未绑定商品',
							success: function(res) {
								if (res.confirm) {
									let url = {
										path: '/pagesContrast/uncheckGoodsList/uncheckGoodsList', 
										query: { 
											SupplierCode: that.$Route.query.SupplierCode,
											SupervisionCode: that.searchVal,
											scanType:1
										}
									}
									that.$Router.push(url)
								} else if (res.cancel) {
						
								}
							}
						});
					}
				}).catch( res => {
					uni.hideLoading()
					that.$common.showNormalToast(res);
				})
			},
			openPopup(popup) {
				this.$refs[popup].open()
			},
			closePopup(popup) {
				this.$refs[popup].close()
			},
			closeAll(){				
				that.closePopup('recheckPopup')
			},
			reCheck() {
				that.closePopup('successPopup')
				that.openPopup('recheckPopup')
			},
			closeSuccessPopup() {
				that.closePopup('successPopup')
				that.cleanCode()
			},
			closeRecheckPopup() {
				that.closePopup('recheckPopup')
				that.inputFoucs()
			},
			confirmRecheck() {
				that.deleteHisGood()
			},
			deleteHisGood() {
				// 取消对码				
				let requestData = {
					Code: that.searchVal.substring(0,7)					
				};
				//console.log(requestData)
				that.$request.globalRequest('goods/cancel', 'POST', requestData, '', 0).then(res => {
						that.$common.showNormalToast('已取消');			
						 setTimeout(function(){
							 let url = {
							 	path: '/pagesContrast/uncheckGoodsList/uncheckGoodsList', 
							 	query: { 
							 		SupplierCode: that.$Route.query.SupplierCode,
							 		SupervisionCode: that.searchVal,
									scanType:1
							 	}
							 }
							 that.closePopup('recheckPopup')
							 that.$Router.push(url)
						 },1000)	
				}).catch( res => {
					that.$common.showNormalToast(res);
				})
			},
			inputFoucs() {
				uni.hideKeyboard()
				that.isFocus =  false
				setTimeout(() => {
					that.isFocus = true
				}, 100)
			},
		}
	}
</script>

<style lang="scss" scoped>
@import "goodsContrast.scss";
</style>
